/**
 * Created by test on 2019/4/17.
 */
var pn = 1;
var page = 10;
var hasmore = true;
$(function(){
    getchat();
    $('.content').scroll(function(){
        if($(this).scrollTop()<10){
            getchat();
        }
    });
    $('.content').stop().animate({
        scrollTop: $('.content')[0].scrollHeight
    }, 800);
    var url = "ws://45.40.207.184:9502";
    var socket = '';
    function Connect(){
        //兼容 FireFox
        if ("WebSocket" in window) {
            socket = new WebSocket(url);
        } else if ("MozWebSocket" in window) {
            socket = new MozWebSocket(url);
        }
    }
    Connect();
    socket.onopen = function(event) {
        console.log('连接成功!');
    };
    socket.onmessage = function(event) {//当客户端收到服务端发来的消息时，会触发onmessage事件，参数event.data中包含server传输过来的数据
        var content = event.data;//获取消息
        content = JSON.parse(content);
        //消息信息
        if (content.type == 'message') {
            var message = content.data;
            if (Object.keys(message).length > 0 && message.chat_content != '') {
                var str = '<div class="chatcontent"><p class="imp">'+
                    message.chat_content+
                    '</p>' +
                    '<p class="time">'+
                    message.add_time
                    +'  '+message.chat_user_name+
                    '</p></div>' ;
                $('.content').append(str);
                $('.content').stop().animate({
                    scrollTop: $('.content')[0].scrollHeight
                }, 800);
            }
        }

        //提醒类型消息
        if (content.type == 'onlineList') {
            console.log('当前在线人数:',content.data);
        }

    };
    socket.onclose = function(evt)
    {
        console.log("WebSocketClosed!");
        Connect();
    };
    socket.onerror = function(evt)
    {
        console.log("WebSocketError!");
        Connect();
    };
    //发送消息
    $('#send').on('click',function(){
        var content = $('#message').val();
        if(content.length > 0){
            var data = {content:content};
            $.ajax({
                url:'/chat',
                data:data,
                type:'post',
                dataType:'json',
                success:function(res){
                    if(res.state == 200){
                        $('#message').val('');
                    }else{
                        alert('发送失败');
                    }

                }
            });

        }
    });
    $(document).keypress(function(e) {
        if((e.keyCode || e.which)==13) {
            $("#send").click();
        }
    });
});
//获取聊天记录
function getchat()
{
    if(!hasmore){
        return;
    }
    $.ajax({
        url:'/chatlist',
        data:{pn:pn,page:page},
        type:'get',
        dataType:'json',
        success:function(res){
            console.log(res);
            if(res.state == 200 && res.data.length>0){
                var str = '';
                $.each(res.data,function(k,v){
                    str += '<div class="chatcontent">' +
                    '<p class="imp">'+v.chat_content+'</p>' +
                    '<p class="time">'+v.add_time+'  '+v.chat_user_name+'</p>' +
                    '</div>';
                });
                $('.content').prepend(str);
                pn++;
            }else{
                hasmore = false;
            }
        }
    });
}